package com.kdtech.analyse.Bbs;
import com.kdtech.crawler.at.UrlArgumentTop;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.DoMainUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.NumberUtils;
import com.kdtech.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.kdtech.crawler.CrawlHTML;

import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.analyse.AnalyseNews;

/**
 * 福州便民论坛
 * @author Chase
 *
 */
public class FzbmBbsAnalyse implements AnalyseNews {
//http://bbs.0757qq.com/read-htm-tid-185736.html
	private static final String[] regex = {
		"http://bbs.fzbm.com/forum-[0-9]*/tid-[0-9]*.html"
	};


	
	public boolean isDetailPage(String url) {
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return false;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {
		NewsMeta meta = new NewsMeta();
		if (urlMeta.getHtml() == null) {
		}
		String url = urlMeta.getUrl();
		/*
		 * 网站源码
		 */
		String htmltxt = urlMeta.getHtml();
		String title = null;
		String content = null;
		String author = null;
		Long date = null;
		Integer commnetNum = null;
		Integer clickNum = null;
		Document doc = Jsoup.parse(htmltxt);

		/*
		 * 标题
		 */
		title = doc.select("div.con-box-title h3").text();

		/*
		 * 时间
		 */
		date = DateUtils.matchDate(doc.select("div.con-box-title").text());

		/*
		 * 内容
		 */
		content = HtmlCleaner.getContentHtml(url,  doc.select("div.con-box-one div#contentBox"));

		/*
		 * 作者
		 */
		author = doc.select("div.con-box-title span.span-one").text();


		if (clickNum == null && commnetNum==null ){
			clickNum = NumberUtils.matchNumber(doc.select("div.con-box-title em").get(0).text());
			commnetNum = NumberUtils.matchNumber(doc.select("div.con-box-title em").get(1).text());
		}
		/*
		 * 链接
		 */
		meta.setUrl(url);

		/*
		 * 来源
		 */
		meta.setTitle(title);
		meta.setContent(content);
		meta.setAuthor(author);
		meta.setDate(date);
		meta.setUpdateUrl(url);
		meta.setClickNum(clickNum);
		meta.setCommentNum(commnetNum);
		return meta;
	}

	

	
	public NewsMeta Update(NewsMeta meta) {
		if(meta!=null){
			String updateUrl = meta.getUpdateUrl();
			if(StringUtils.isNotBlank(updateUrl)){
				UrlMeta urlMeta  = CrawlHTML.responseToURL(updateUrl);
				return parserHtml(urlMeta);
			}
		}
		return null;
	}


	
}
